package com.so206.config;

import com.so206.utils.MyBeetlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.dialect.IExpressionObjectDialect;
import org.thymeleaf.expression.IExpressionObjectFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * Thymeleaf 自定义工具类
 */
@Component
public class UtilDialect implements IExpressionObjectDialect {

    private static final Logger logger = LoggerFactory.getLogger(UtilDialect.class);

    @Autowired
    private MyBeetlUtils myBeetlUtils;

    @Override
    public String getName() {
        return "pay";
    }

    public UtilDialect() {
        logger.info("UtilDialect Init...");
    }

    @Override
    public IExpressionObjectFactory getExpressionObjectFactory() {
        return new IExpressionObjectFactory() {

            @Override
            public Set<String> getAllExpressionObjectNames() {
                // 可为自定义工具类起多个名称 一个名称初始化一个对象
                return new HashSet<String>() {{
                    add("pay");
                }};
            }

            @Override
            public Object buildObject(IExpressionContext context, String expressionObjectName) {
                return myBeetlUtils;
            }

            @Override
            public boolean isCacheable(String expressionObjectName) {
                return true;
            }
        };
    }


}
